//
//  HomeViewController.swift
//  MQLWeibo
//
//  Created by MQL-IT on 2016/10/23.
//  Copyright © 2016年 MQL-IT. All rights reserved.
//

import UIKit
import SwiftyJSON

//定义全局常量,尽量使用私有,仅在本文件中使用
private let cellId = "HomeCellid"

class HomeViewController: BaseViewController {
    
    //懒加载微博数据数组,懒加载无需考虑实例化和解包;
    fileprivate lazy var statusList = [StatuseModel]()
    fileprivate lazy var mitView: MITSycleScrollView = MITSycleScrollView(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: UIScreen.main.bounds.size.width / 2))
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
    }
    //加载数据
    override func loadData() {
        
        
        NetWorkManager.shared.homeStatusList { (response, isSuccess) in
            
            self.refreshControl?.endRefreshing()
            guard let json = response else {
                
                return
            }
            
            let mmmm = JSON(json)
            let model = StatusesHomeModel(fromJson: mmmm)
            if self.isPullUp {
                //加载数据结束,结束刷新控件
                self.statusList.append(contentsOf: model.statuses)
                self.isPullUp = false
            } else {
                self.statusList = model.statuses
            }
            self.tableView?.reloadData()
        }
    }
    //显示好友
    @objc fileprivate func showFriends() {
        print(#function)
        let vc = TestViewController()
        navigationController?.pushViewController(vc, animated: true)
    }

}
//MARK: - 表格数据源方法,具体的数据源方法,不需要super
extension HomeViewController {
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //1.取cell
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! TableViewCell
        //2.设置cell
        let model = statusList[indexPath.row]
        
        cell.avatarView.kf.setImage(with: URL(string: model.user.avatarLarge))
        cell.NameLabel.text = model.user.name
        cell.desLabel.text = model.user.descriptionField
        //3.返回cell
        return cell
    }
    
}
//MARK: - 设置界面
extension HomeViewController {
    //重写父类方法
    override func setupTableView() {
        super.setupTableView()
        tableView?.rowHeight = 100
        //注册原型cell
        
        tableView?.register(UINib(nibName: "TableViewCell", bundle: nil), forCellReuseIdentifier: cellId)
//        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
        //设置导航栏
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        loadData()
        setupBannerView()
    }
    
    private func setupBannerView() {
        mitView.currentPageColor = UIColor.red
        mitView.pageColor = UIColor.white
        mitView.timeSeconds = 2
        mitView.pageAlignment = .right
        mitView.placeHolderImage = #imageLiteral(resourceName: "visitordiscover_signup_logo")
        mitView.delegate = self
        //给轮播图赋值
        mitView.imageArray = ["http://bangimg1.dahe.cn/forum/201511/05/073002qffz5t9twjjbaz6m.jpg","http://tupian.enterdesk.com/2015/gha/09/1502/01.jpg","http://img.ithome.com/newsuploadfiles/2016/3/20160317_080446_325.jpg"]
        tableView?.tableHeaderView = mitView
    }
}

extension HomeViewController: MITSycleScrollViewDelegate {
    func didClickBannerIndex(index: Int) {
        printLog(message: "点击了第\(index)个Banner")
    }
}




